//using System.Web.Services.Protocols;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Serialization;

namespace Unis.Framework.Core.Utility
{
	public class Serialization
	{
		public static byte[] Serialize(object source)
		{
			using(MemoryStream ms = new MemoryStream())
			{
				BinaryFormatter bf = new BinaryFormatter();
				bf.Serialize(ms, source);
				return ms.ToArray();
			}
		}

        public static string Serialize<T>(T data)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            using (MemoryStream s = new MemoryStream())
            {
                xmlSerializer.Serialize(s, data);
                UTF8Encoding utf8 = new UTF8Encoding(false, true);
                return utf8.GetString(s.ToArray());
            }
        }

		public static object Deserialize(byte[] source)
		{
			using(MemoryStream ms = new MemoryStream(source))
			{
				BinaryFormatter bf = new BinaryFormatter();
				return (object)bf.Deserialize(ms);
			}
		}
	}
}
